package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import sk.stuba.fiit.repka.dp.owl.MyOWLClass;
import sk.stuba.fiit.repka.dp.owl.OWLComponent;

public class CreateIndividualDialog extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JComboBox comboBox;
	private List<MyOWLClass> inputForCombo;
	private String outputName;
	private MyOWLClass outputClass;
	
	public String getOutputName() {
		return this.outputName;
	}
	
	public MyOWLClass getOutputCLass() {
		return this.outputClass;
	}

	/**
	 * Create the dialog.
	 */
	public CreateIndividualDialog(List<MyOWLClass> classes) {
		this.inputForCombo = new ArrayList<MyOWLClass>(classes);
		this.setModal(true);
		setTitle("Create Individual");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lblIndividualName = new JLabel("Individual name");
			lblIndividualName.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lblIndividualName = new GridBagConstraints();
			gbc_lblIndividualName.insets = new Insets(80, 50, 10, 20);
			gbc_lblIndividualName.anchor = GridBagConstraints.WEST;
			gbc_lblIndividualName.gridx = 0;
			gbc_lblIndividualName.gridy = 0;
			contentPanel.add(lblIndividualName, gbc_lblIndividualName);
		}
		{
			textField = new JTextField();
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.anchor = GridBagConstraints.WEST;
			gbc_textField.insets = new Insets(80, 20, 5, 0);
			gbc_textField.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField.gridx = 1;
			gbc_textField.gridy = 0;
			contentPanel.add(textField, gbc_textField);
			textField.setColumns(20);
		}
		{
			JLabel lblIndividualType = new JLabel("Individual type");
			lblIndividualType.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lblIndividualType = new GridBagConstraints();
			gbc_lblIndividualType.anchor = GridBagConstraints.WEST;
			gbc_lblIndividualType.insets = new Insets(0, 50, 10, 20);
			gbc_lblIndividualType.gridx = 0;
			gbc_lblIndividualType.gridy = 1;
			contentPanel.add(lblIndividualType, gbc_lblIndividualType);
		}
		{
			comboBox = new JComboBox(this.initializeCombo(inputForCombo));
			GridBagConstraints gbc_comboBox = new GridBagConstraints();
			gbc_comboBox.anchor = GridBagConstraints.WEST;
			gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBox.gridx = 1;
			gbc_comboBox.gridy = 1;
			contentPanel.add(comboBox, gbc_comboBox);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	private String[] initializeCombo(List<MyOWLClass> inputCombo) {
		String[] result = new String[inputCombo.size()];
		int iter = 0;
		for(MyOWLClass o : inputCombo) {
			result[iter++] = o.getName();
		}
		return result;
	}
	
	//find OWLClass as type of individual
	private MyOWLClass findClassByName(String className, List<? extends OWLComponent> propertyList) {
		OWLComponent result = null;
		Iterator<? extends OWLComponent> iter = propertyList.iterator();
		while(iter.hasNext()) {
			OWLComponent o = iter.next();
			if(className.equals(o.getName())) {
				result = o;
			} else if(o.hasSubComponent()) {
				result = this.findClassByName(className, o.getSubComponents());
			}
		}
		return (MyOWLClass)result;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			this.outputName = this.textField.getText();
			this.outputClass = this.findClassByName((String)this.comboBox.getSelectedItem(), this.inputForCombo);
			this.dispose();
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		} 
	}
}
